package com.dufang.ThreadWaitAndNotify;

/**
 * @ClassName:Product
 * @Description 生产者生产包子
 * @Author Administrator
 * @Date 2025/7/7/007 15:38
 * @Version 1.0
 **/
public class Product implements Runnable {
    //这种定义导致消费和并不在一个锁对象进程里面
  //  BaoZiPu baoZiPu=new BaoZiPu();
    private BaoZiPu baoZiPu;//对象成员变量

    public Product(BaoZiPu baoZiPu){
        this.baoZiPu=baoZiPu;
    }
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            baoZiPu.setCount();
           /* synchronized (baoZiPu) {
                //1、判断flag是否为true,如果是true,证明有包子，生产线程等待
                if (baoZiPu.isFlag() == true) {
                    try {
                        baoZiPu.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                //2、如果flag为false,证明没有包子，开始生产
                baoZiPu.setCount();//可将这里的同步代码放进setCount里面
                //3、改变flag状态，为true,证明生产完了，有包子了
                baoZiPu.setFlag(true);
                //4、唤醒消费线程
                baoZiPu.notify();
            }*/
        }
    }
}